package org.edu.modules.research.rproject.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.edu.common.dto.ExcelPo;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.utils.PoiUtils;
import org.edu.modules.research.rbase.entity.VeJkyUser;
import org.edu.modules.research.rproject.dto.VeJkyProjectDto;
import org.edu.modules.research.rproject.entity.VeJkyProject;
import org.edu.modules.research.rproject.entity.VeJkyProjectWork;
import org.edu.modules.research.rproject.service.VeJkyProjectService;
import org.edu.common.api.vo.Result;
import org.edu.common.base.PageDto;
import org.edu.common.dto.StatusDto;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.query.QueryGenerator;
import org.edu.modules.research.rproject.vo.VeJkyUserCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Api(tags = {"科研项目信息表"})
@RestController
@RequestMapping("/research/veJkyProject")
@Slf4j
public class VeJkyProjectController{

    @Autowired
    private VeJkyProjectService veJkyProjectService;

    @ApiOperation("分页查询科研项目信息表")
    @PostMapping("/page")
    public Result<?> page(@RequestBody PageDto<VeJkyProject> page) {
        QueryWrapper<VeJkyProject> queryWrapper =  page.buildQueryWrapper(VeJkyProject.class);

        if(page.getParams()!=null && page.getParams().containsKey("userId")){
            queryWrapper.apply(" d.id in( select project_id from ve_jky_project_user where jky_user_id={0} ) ",
                    page.getParams().get("userId"));
        }

        return Result.OK(veJkyProjectService.pageDto(page, queryWrapper));
    }


    @ApiOperation("导出数据）")
    @PostMapping("/export")
    public Result<?> export(@RequestBody PageDto<VeJkyProject> page, HttpServletResponse resp) {
        QueryWrapper<VeJkyProject> queryWrapper =  page.buildQueryWrapper(VeJkyProject.class);
        List<ExcelPo> list = new ArrayList<>();
        list.add(new ExcelPo("项目编号","serial"));
        list.add(new ExcelPo("项目名称","name"));
        list.add(new ExcelPo("项目进度","progress",data->{
            return data+"%";
        }));
        list.add(new ExcelPo("项目类型","cateIdText"));
        list.add(new ExcelPo("项目级别","levelIdText"));
        list.add(new ExcelPo("项目总经费","totalCost"));
        list.add(new ExcelPo("项目进度","progress"));
        list.add(new ExcelPo("立项组织","createOragnText"));
        list.add(new ExcelPo("负责人","chargeUidText"));
        list.add(new ExcelPo("项目合同号","termNum"));
        list.add(new ExcelPo("项目来源","source"));
        list.add(new ExcelPo("开始时间","startTime"));
        list.add(new ExcelPo("结束时间","endTime"));
        list.add(new ExcelPo("审批人","mark"));
        list.add(new ExcelPo("审核时间","checkTime"));
        list.add(new ExcelPo("项目成员","userNames"));

        PoiUtils.downloadFile(resp,veJkyProjectService.page(page, queryWrapper).getRecords(),list);
        return Result.OK();
    }

    @ApiOperation("分页项目统计")
    @PostMapping("/countProject")
    public Result<?> countProject(@RequestBody PageDto<VeJkyProjectDto> page) {
        QueryWrapper<VeJkyProject> queryWrapper =  page.buildQueryWrapper(VeJkyProject.class);
        return Result.OK(veJkyProjectService.countProject(page, queryWrapper));
    }
    @ApiOperation("分页项目统计")
    @PostMapping("/countProjectExport")
    public Result<?> countProjectExport(@RequestBody PageDto<VeJkyProjectDto> page, HttpServletResponse resp) {
        QueryWrapper<VeJkyProject> queryWrapper =  page.buildQueryWrapper(VeJkyProject.class);
        List<ExcelPo> list = new ArrayList<>();
        list.add(new ExcelPo("项目编号","serial"));
        list.add(new ExcelPo("项目名称","name"));
        list.add(new ExcelPo("项目进度","progress",data->{
            return data+"%";
        }));
        list.add(new ExcelPo("项目类型","cateIdText"));
        list.add(new ExcelPo("项目级别","levelIdText"));
        list.add(new ExcelPo("立项组织","createOragnText"));
        list.add(new ExcelPo("项目来源","source"));
        list.add(new ExcelPo("项目成员","userNames"));
        list.add(new ExcelPo("负责人","chargeUidText"));
        list.add(new ExcelPo("项目成果个数","fruitNum"));
        list.add(new ExcelPo("项目工作个数","workNums"));
        list.add(new ExcelPo("开始时间","startTime"));
        list.add(new ExcelPo("结束时间","endTime"));
        PoiUtils.downloadFile(resp,veJkyProjectService.countProject(page, queryWrapper).getRecords(),list);
        return Result.OK();
    }

    @ApiOperation("分页根据人员统计项目")
    @PostMapping("/countProjectUser")
    public Result<?> countProjectUser(@RequestBody PageDto<VeJkyUserCountVo> page) {
        QueryWrapper<VeJkyProject> queryWrapper =  page.buildQueryWrapper(VeJkyProject.class);
        return Result.OK(veJkyProjectService.countProjectUser(page, queryWrapper));
    }

    @ApiOperation("分页项目统计")
    @PostMapping("/countProjectUserExport")
    public Result<?> countProjectUserExport(@RequestBody PageDto<VeJkyUserCountVo> page, HttpServletResponse resp) {
        QueryWrapper<VeJkyProject> queryWrapper =  page.buildQueryWrapper(VeJkyProject.class);
        List<ExcelPo> list = new ArrayList<>();
        list.add(new ExcelPo("姓名","realname"));
        list.add(new ExcelPo("毕业院校","graCollege"));
        list.add(new ExcelPo("学历","educate",data->{
            return data+"%";
        }));
        list.add(new ExcelPo("职称","proCall"));
        list.add(new ExcelPo("项目级别","levelIdText"));
        list.add(new ExcelPo("科研方向","direction"));
        list.add(new ExcelPo("参与项目个数","projectNum"));
        list.add(new ExcelPo("项目成果个数","fruitNum"));
        list.add(new ExcelPo("工作个数","workNums"));
        PoiUtils.downloadFile(resp,veJkyProjectService.countProjectUser(page, queryWrapper).getRecords(),list);
        return Result.OK();
    }


    @AutoLog(value = "科研项目信息表-分页列表查询")
    @ApiOperation(value="科研项目信息表-分页列表查询", notes="科研项目信息表-分页列表查询")
    @PostMapping(value = "/list")
    public Result<?> queryPageList(VeJkyProject entity,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeJkyProject> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        PageDto<VeJkyProject> page = new PageDto<VeJkyProject>(pageNo, pageSize);
        IPage<VeJkyProject> pageList = veJkyProjectService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog(value = "通过id查询")
    @ApiOperation(value="通过id查询", notes="通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        VeJkyProject dto = veJkyProjectService.findDtoById(id);
        if(dto==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(dto);
    }

    @AutoLog(value = "添加")
    @ApiOperation(value="添加", notes="添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeJkyProjectDto dto) {
        veJkyProjectService.insertOrUpdate(dto , true);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "编辑")
    @ApiOperation(value="编辑", notes="编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeJkyProjectDto dto) {
        veJkyProjectService.insertOrUpdate(dto , false);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "批量删除")
    @ApiOperation(value="批量删除", notes="批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        veJkyProjectService.delete(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }


    @AutoLog(value = "更新状态")
    @ApiOperation(value="更新状态")
    @PostMapping(value = "/updateStatus")
    public Result<?> updateStatus(@RequestBody StatusDto statusDto) {
        UpdateWrapper<VeJkyProject> updateWrapper = new UpdateWrapper<>();
        if(statusDto.getIds()!=null && statusDto.getIds().size()>0){
            updateWrapper.set("status",statusDto.getStatus());
            updateWrapper.in("id",statusDto.getIds());
            veJkyProjectService.update(updateWrapper);
            return Result.OK("批量更新成功!");
        }else{
            return Result.error("未选择!");
        }
    }



    @AutoLog(value = "审核")
    @ApiOperation(value="审核")
    @PostMapping(value = "/audit")
    public Result<?> updateStatus(@RequestBody VeJkyProjectDto dto) {
        UpdateWrapper<VeJkyProject> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("check_status",dto.getCheckStatus());
        updateWrapper.set("check_txt",dto.getCheckTxt());
        updateWrapper.set("check_time",new Date());

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        updateWrapper.set("mark",sysUser.getRealname());
        updateWrapper.set("mark_uid",sysUser.getId());
        updateWrapper.eq("id",dto.getId());
        veJkyProjectService.update(updateWrapper);
        return Result.OK("审核成功!");
    }

    @AutoLog(value = "更新进度")
    @ApiOperation(value="更新进度")
    @PostMapping(value = "/updateProgress")
    public Result<?> updateProgress(@RequestBody StatusDto statusDto) {
        UpdateWrapper<VeJkyProjectWork> updateWrapper = new UpdateWrapper<>();
        if(statusDto.getIds()!=null && statusDto.getIds().size()>0){
            updateWrapper.set("progress",statusDto.getStatus());
            updateWrapper.in("id",statusDto.getIds());
            veJkyProjectService.update(updateWrapper);
            return Result.OK("更新进度成功!");
        }else{
            return Result.error("未选择!");
        }
    }


}
